﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Portal_Tech
{
    class Serializer
    {
        public Serializer()
        {
        }

        //save
        public bool saveGroups(List<SubGroup> groups)
        {
            Stream stream;
            try
            {
                stream = File.Open("g_save.ptf", FileMode.Create);
            }
            catch (IOException e)
            {
                return false;
            }

            BinaryFormatter bFormatter = new BinaryFormatter();
            try
            {
                bFormatter.Serialize(stream, groups);
                stream.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public bool saveFloats(List<ParadeFloat> floats)
        {
            Stream stream;
            try
            {
                stream = File.Open("f_save.ptf", FileMode.Create);
            }
            catch (IOException e)
            {
                return false;
            }

            BinaryFormatter bFormatter = new BinaryFormatter();
            try
            {
                bFormatter.Serialize(stream, floats);
                stream.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public List<SubGroup> readGroups()
        {
            List<SubGroup> list = new List<SubGroup>();

            Stream stream = File.Open("g_save.ptf", FileMode.Open);


            BinaryFormatter bFormatter = new BinaryFormatter();
            try
            {
                list = (List<SubGroup>)bFormatter.Deserialize(stream);
                stream.Close();
            }
            catch (Exception e)
            {
                list = null;
            }
            return list;
        }

        public List<ParadeFloat> readFloats()
        {
            List<ParadeFloat> list = new List<ParadeFloat>();

            Stream stream = File.Open("f_save.ptf", FileMode.Open);


            BinaryFormatter bFormatter = new BinaryFormatter();
            try
            {
                list = (List<ParadeFloat>)bFormatter.Deserialize(stream);
                stream.Close();
            }
            catch (Exception e)
            {
                list = null;
            }
            return list;
        }
    }
}
